package com.sourceQ.system.controller.tenant;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sourceQ.common.core.constant.dict.BmTenantPackageStatus;
import com.sourceQ.common.core.domain.R;
import com.sourceQ.common.core.utils.poi.ExcelUtil;
import com.sourceQ.common.core.web.controller.BaseController;
import com.sourceQ.common.core.web.domain.AjaxResult;
import com.sourceQ.common.log.annotation.Log;
import com.sourceQ.common.log.enums.BusinessType;
import com.sourceQ.system.domain.tenant.TenantPackage;
import com.sourceQ.system.dto.tenant.TenantPackagePageReqDTO;
import com.sourceQ.system.vo.tenant.TenantPackageRespVO;
import com.sourceQ.system.dto.tenant.TenantPackageSaveReqDTO;
import com.sourceQ.system.service.tenant.TenantPackageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author: SourceQ
 * @create: 2024-04-01 10:19
 * @Description:
 */
@RestController
@RequestMapping(value = "/tenant-package")
@Api(tags = "租户套餐管理")
public class TenantPackageController extends BaseController {

    @Autowired
    private TenantPackageService tenantPackageService;
    @Log(title = "租户套餐管理", businessType = BusinessType.INSERT)
    @ApiOperation(value = "租户套餐新增")
    @PostMapping("/create")
    public AjaxResult create(@RequestBody TenantPackageSaveReqDTO createReqVO){
        return toAjax(tenantPackageService.createTenantPackage(createReqVO));
    }
    @Log(title = "租户套餐管理", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "租户套餐修改")
    @PutMapping("/update")
    public AjaxResult update(@RequestBody TenantPackageSaveReqDTO updateReqVo){
        tenantPackageService.updateTenantPackage(updateReqVo);
        return toAjax(true);
    }
    @Log(title = "租户套餐管理", businessType = BusinessType.QUERY)
    @ApiOperation(value = "租户套餐列表查询")
    @GetMapping("/page")
    public R<IPage<TenantPackage>> getListPage(Page page, TenantPackagePageReqDTO vo){
        return R.ok(tenantPackageService.getTenantPackagePage(page, vo));
    }
    @Log(title = "租户套餐管理", businessType = BusinessType.DELETE)
    @ApiOperation(value = "租户套餐删除")
    @DeleteMapping("/{ids}")
    public R<Void> delete(@PathVariable Long[] ids){
        tenantPackageService.deleteTenantPackage(ids);
        return R.ok();
    }
    @ApiOperation(value = "租户套餐详情")
    @GetMapping("/detail/{id}")
    public AjaxResult getInfo(@PathVariable Long id){
        return success(tenantPackageService.getTenantPackage(id));
    }

    @ApiOperation(value = "套餐导出")
    @Log(title = "租户套餐管理",businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TenantPackagePageReqDTO dto){
        List<TenantPackageRespVO> list = tenantPackageService.getTenantPackageVoList(dto);
        ExcelUtil<TenantPackageRespVO> util = new ExcelUtil<>(TenantPackageRespVO.class);
        util.exportExcel(response, list, "租户套餐数据");
    }
    @Log(title = "租户套餐管理",businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改套餐状态")
    @GetMapping("/change-status")
    public AjaxResult updateTenantPackageStatus(Long id,String status){
        tenantPackageService.changeTenantPackage(id, status);
        return success();
    }
    @ApiOperation(value = "获取全部已启用套餐")
    @GetMapping("/get-package-status")
    public AjaxResult getPackageAll(){
        return success(tenantPackageService.getTenantPackageListByStatus(BmTenantPackageStatus.NORMAL));
    }

}
